var mongo = require("../mongo/mongo");
const mongoose = require('mongoose');

exports.hasPower = function (req, o) {
    // body...


};



exports.isLogin = function (req) {
    // body...
    if (!req) {
       throw '判断登录必须传递req[isLogin]'
    }
    if (!req.session) {
        return false

    }


    if (!req.session.member) {
        return false
    }

    return true



};

exports.getMe = async function (req) {
    // body...


    if (this.isLogin(req)) {

        var member = req.session.member;
        await this.countMember(member._id)
        member = await mongo.findOneAsync({
            data: {
                _id: member._id

            },
            collection: "Member"
        })

        return member


    }
    else {
        return null
    }

};

//统计用户
exports.countMember = async function (memberId) {
    // body...

    //未完成的任务数量
    var newTaskNum = await mongo.countAsync({
        data: {
            $or: [{ "managers._id": memberId }, { "members._id": memberId }],
            statusId: { $lt: 300 }
        }, collection: "task"
    })

    await mongo.updateAsync({ _id: mongoose.Types.ObjectId(memberId) }, { $set: { newTaskNum: newTaskNum } }, "Member")


    return {
        newTaskNum: newTaskNum
    }

};


